package com.sso.managedBean;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicSystem;
import com.sso.service.IDicSystemService;

@ManagedBean(name = "sso_editSystemBean", scope = ManagedBeanScope.REQUEST)
public class EditSystemBean {
	protected IDicSystemService dicSystemService = SpringBeanHolder
			.getBean("dicSystemService");

	@Bind
	@Accessible
	@SaveState
	protected String systemID;
	protected DicSystem system;
	// 系统名称
	@Bind(id = "systemName", attribute = "value")
	protected String systemName;
	// 描述
	@Bind(id = "systemRemark", attribute = "value")
	protected String systemRemark;
	//系统地址
	@Bind(id = "systemUrl", attribute = "value")
	protected String systemUrl;
	//系统服务地址
	@Bind(id = "systemServiceAddress", attribute = "value")
	protected String systemServiceAddress;

	@Inject
	private FacesContext context;

	@SaveState
	private String first;

	@BeforeRender
	public void beforeRender(boolean isPostBack) throws Exception {
		if (!isPostBack) {
			systemID = context.getExternalContext().getRequestParameterMap()
					.get("id");
			if (!"".equals(systemID) && null != systemID) {
				system = dicSystemService.getBySystemID(systemID);
				if (system != null) {
					systemID = system.getId();
					systemName = system.getSystemName();
					systemUrl = system.getSystemUrl();
					systemServiceAddress = system.getSystemServiceAddress();
					systemRemark = system.getSystemRemark();
				}
			}
		}
	}

	@Action(id = "save")
	public void save() {
		system = dicSystemService.getBySystemID(systemID);
		system.setId(systemID);
		system.setSystemName(systemName);
		system.setSystemUrl(systemUrl);
		system.setSystemServiceAddress(systemServiceAddress);
		system.setSystemRemark(systemRemark);
		dicSystemService.saveDicSystem(system);
		Browser
				.execClientScript("javascript:closeCenterLayoutTab('eidtSystemTab')");
	}
}
